package com.qi.services.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qi.common.RoleKey;
import com.qi.exception.ServerException;
import com.qi.mapper.ClubMapper;
import com.qi.pojo.dto.AddClubBody;
import com.qi.pojo.dto.UpdateClubBody;
import com.qi.pojo.entity.*;
import com.qi.pojo.vo.UserDetails;
import com.qi.services.IClubService;
import com.qi.services.IRoleService;
import com.qi.services.ITypeService;
import com.qi.services.IUserService;
import com.qi.utils.ImageUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class ClubServiceImpl extends ServiceImpl<ClubMapper, Club> implements IClubService {

    @Resource
    private IRoleService roleService;

    @Resource
    private IUserService userService;

    @Resource
    private ITypeService typeService;

    @Override
    public void add(AddClubBody addClubBody) {
        // 构造社团实体类
        Club club = new Club();
        BeanUtil.copyProperties(addClubBody, club);
        // 插入数据库
        this.save(club);
        // 编辑社长信息
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<Role>().eq(Role::getRoleKey, RoleKey.社团管理员);
        Role role = roleService.getOne(roleLambdaQueryWrapper);
        User user = userService.getById(addClubBody.getPresidentId());
        user.setClubId(club.getId());
        user.setRoleId(role.getId());
        userService.updateById(user);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
        LambdaQueryWrapper<Role> roleQueryWrapper = new LambdaQueryWrapper<Role>()
                .eq(Role::getRoleKey, RoleKey.游客);
        Long roleId = roleService.getOne(roleQueryWrapper).getId();
        // 修改社员的角色、社团id
        LambdaUpdateWrapper<User> userUpdateWrapper = new LambdaUpdateWrapper<User>()
                .set(User::getRoleId, roleId)
                .set(User::getClubId, 0L)
                .eq(User::getClubId, id);
        userService.update(userUpdateWrapper);
    }

    @Override
    public void modify(UpdateClubBody updateClubBody) {
        // 数据校验
        if (ObjUtil.isEmpty(updateClubBody)
                || ObjUtil.isEmpty(updateClubBody.getId())) {

            throw new ServerException("修改社团业务：社团id不能为空");
        }
        // 查询社团信息
        Club club = this.getById(updateClubBody.getId());

        if (ObjUtil.equals(updateClubBody.getName(), club.getName())
                && ObjUtil.equals(updateClubBody.getTypeId(), club.getTypeId())
                && ObjUtil.equals(updateClubBody.getPresidentId(), club.getPresidentId())
                && StrUtil.isBlank(updateClubBody.getImageLocation())) {

            throw new ServerException("没有编辑任何信息，无需修改");
        }
        // 是否修改社团管理员
        boolean presidentModify = !ObjUtil.equals(updateClubBody.getPresidentId(), club.getPresidentId());
        // 是否修改社团类型
        boolean typeModify = !ObjUtil.equals(updateClubBody.getTypeId(), club.getTypeId());
        if (presidentModify) {
            // 查询角色信息
            LambdaQueryWrapper<Role> roleClubAdminLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roleClubAdminLambdaQueryWrapper.eq(Role::getRoleName, "社团管理员");
            LambdaQueryWrapper<Role> roleClubUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roleClubUserLambdaQueryWrapper.eq(Role::getRoleName, "社团成员");
            Long clubAdminRoleId = roleService.getOne(roleClubAdminLambdaQueryWrapper).getId();
            Long clubUserRoleId = roleService.getOne(roleClubUserLambdaQueryWrapper).getId();
            // 撤销原社团管理员职务
            LambdaUpdateWrapper<User> userRevokeLambdaUpdateWrapper = new LambdaUpdateWrapper<User>()
                    .set(User::getRoleId, clubUserRoleId)
                    .eq(User::getId, club.getPresidentId());
            userService.update(userRevokeLambdaUpdateWrapper);
            // 授权新社团管理员职务
            LambdaUpdateWrapper<User> userAuthorizeLambdaUpdateWrapper = new LambdaUpdateWrapper<User>()
                    .set(User::getClubId, club.getId())
                    .set(User::getRoleId, clubAdminRoleId)
                    .eq(User::getId, updateClubBody.getPresidentId());
            userService.update(userAuthorizeLambdaUpdateWrapper);
        }

        // 构造修改规则
        LambdaUpdateWrapper<Club> clubLambdaUpdateWrapper = new LambdaUpdateWrapper<Club>()
                .set(!StrUtil.isBlank(updateClubBody.getName()), Club::getName, updateClubBody.getName())
                .set(typeModify, Club::getTypeId, updateClubBody.getTypeId())
                .set(presidentModify, Club::getPresidentId, updateClubBody.getPresidentId())
                .set(!StrUtil.isBlank(updateClubBody.getImageLocation()), Club::getImageLocation, updateClubBody.getImageLocation())
                .eq(Club::getId, updateClubBody.getId());
        // 修改数据库
        this.update(clubLambdaUpdateWrapper);
    }

    @Override
    public IPage<Club> listPage(Long current, Long size) {
        IPage<Club> page = new Page<>(current, size);
        page = this.page(page);
        for (Club club : page.getRecords()) {
            club.setImage(ImageUtil.getImageBase64(club.getImageLocation()));
            // 查询社长信息
            User president = userService.getById(club.getPresidentId());
            club.setPresident(president.getName());
            // 查询社团类型
            Type type = typeService.getById(club.getTypeId());
            club.setType(type);
        }
        return page;
    }

    @Override
    public Club getClubById(Long id) {
        if (ObjUtil.isEmpty(id) || ObjUtil.equals(id, 0L)) {
            throw new ServerException("社团id异常");
        }
        Club club = this.getById(id);
        club.setImage(ImageUtil.getImageBase64(club.getImageLocation()));
        // 查询社长信息
        User president = userService.getById(club.getPresidentId());
        club.setPresident(president.getName());
        // 查询社团类型
        Type type = typeService.getById(club.getTypeId());
        club.setType(type);
        return club;
    }
}
